package com.rupertjones.globalcron.server.web;

import com.rupertjones.globalcron.common.domain.Host;
import com.rupertjones.globalcron.common.domain.Setting;
import com.rupertjones.globalcron.common.domain.SettingType;
import com.rupertjones.globalcron.common.domain.User;
import com.rupertjones.globalcron.common.util.StringUtil;
import com.rupertjones.globalcron.server.web.service.JobService;
import com.rupertjones.globalcron.server.web.service.SettingsService;
import com.rupertjones.globalcron.server.web.service.TimezoneService;
import com.rupertjones.globalcron.server.web.service.UserService;
import net.sourceforge.stripes.action.ActionBean;
import net.sourceforge.stripes.action.ActionBeanContext;
import net.sourceforge.stripes.integration.spring.SpringBean;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

import java.util.Collection;
import java.util.List;

/**
 * <p>&copy Rupert Jones 2011,2012</p>
 *
 * @author rup
 */
public abstract class AbstractAction implements ActionBean {

    protected static final String INFO_HOSTS_ACTION         = "InfoHosts.action";
    protected static final String INFO_JOB_ACTION           = "InfoJob.action";
    protected static final String INFO_USERS_ACTION         = "InfoUsers.action";
    protected static final String INFO_SETTINGS_ACTION      = "InfoSettings.action";
    protected static final String HOME_ACTION               = "Home.action";

    protected static final String ADD_JOB_JSP               = "/WEB-INF/jsp/add_job.jsp";
    protected static final String ADD_USER_JSP              = "/WEB-INF/jsp/add_user.jsp";
    protected static final String DELETE_JOB_JSP            = "/WEB-INF/jsp/delete_job.jsp";
    protected static final String HOME_JSP                  = "/WEB-INF/jsp/home.jsp";
    protected static final String EDIT_HOST_JSP             = "/WEB-INF/jsp/edit_host.jsp";
    protected static final String EDIT_JOB_JSP              = "/WEB-INF/jsp/edit_job.jsp";
    protected static final String EDIT_SETTING_JSP          = "/WEB-INF/jsp/edit_setting.jsp";
    protected static final String INFO_SETTINGS_JSP         = "/WEB-INF/jsp/info_settings.jsp";
    protected static final String EDIT_USER_JSP             = "/WEB-INF/jsp/edit_user.jsp";
    protected static final String INFO_AUDIT_LOG_JSP        = "/WEB-INF/jsp/info_audit_log.jsp";
    protected static final String INFO_HOST_JSP             = "/WEB-INF/jsp/info_host.jsp";
    protected static final String INFO_HOSTS_JSP            = "/WEB-INF/jsp/info_hosts.jsp";
    protected static final String INFO_JOB_JSP              = "/WEB-INF/jsp/info_job.jsp";
    protected static final String INFO_JOB_LOG_JSP          = "/WEB-INF/jsp/job_log_info.jsp";
    protected static final String STATISTICS_JSP            = "/WEB-INF/jsp/statistics.jsp";
    protected static final String INFO_USERS_JSP            = "/WEB-INF/jsp/info_users.jsp";
    protected static final String PROFILE_JSP               = "/WEB-INF/jsp/profile.jsp";

    @SpringBean
    private JobService jobService;

    @SpringBean
    private TimezoneService timezoneService;

    private ActionBeanContext context;

    @SpringBean
    private SettingsService settingsService;

    @SpringBean
    private UserService userService;

    @Override
    public void setContext(ActionBeanContext actionBeanContext) {
        this.context = actionBeanContext;
    }

    @Override
    public ActionBeanContext getContext() {
        return context;
    }

    protected JobService getJobService() {
        return jobService;
    }

    protected SettingsService getSettingsService() {
        return settingsService;
    }

    protected UserService getUserService() {
        return userService;
    }

    public Collection<Host> getHosts() {
        return getJobService().getHosts();
    }

    public Collection<Setting> getSettings() {
        return getSettingsService().findAll();
    }

    public boolean isEmpty(String value) {
        return StringUtil.isEmpty(value);
    }

    public String getParameterValue(String key) {
        return getContext().getRequest().getParameter(key);
    }

    public Host getCurrentHost() {
        return jobService.getCurrentHost();
    }

    public List<String> getWorldClock() {
        return timezoneService.currentTimeInCurrentlyUsedZones();
    }

    public boolean isAllowAdHocExecution() {
        return (Boolean) settingsService.getSettingValue(SettingType.ALLOW_AD_HOC_EXECUTION);
    }

    public User getLoggedInUser() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        return userService.findUser((String) authentication.getPrincipal());
    }
}
